<template>
  <div class="reportFormPage">
    <div class="report-dom">
      <el-row>
        <el-col :span="5" :offset="19">
          <div class="pageNumBox">
            <span>第</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.page }}&nbsp;</span>
            <span>页，共</span>
            <span class="placeholder-min text-center">&nbsp;{{ form.totalPage }}&nbsp;</span>
            <span>页</span>
          </div>
        </el-col>
      </el-row>
      <div class="topTwoBox">
        <div class="centerName">
          路基路面平整度试验检测记录表
          <br />(连续式平整度仪)
        </div>
        <div class="codeText">
          <tr>
            JGLP01004c
          </tr>
        </div>
      </div>
      <div class="threeTopBox">
        <div class="left-border">
          <div class="box-title">检测单位名称：</div>
          <div class>{{ form.jiancdwmc }}</div>
        </div>
        <div class="right-border">
          <span>记录编号：</span>
          <span class="placeholder">{{ form.jilbh }}</span>
        </div>
      </div>
      <div class="table-border">
        <!-- .........................1111111111111111111111111111111......................................................... -->
        <table height="220px" class="top-table">
          <tbody>
            <tr>
              <td height="30" align="center" width="15%">
                <span style="width: 90px; display: inline-block">工程名称</span>
              </td>
              <td width="35%" class="text-center">
                <span>{{ form.gongcmc }}</span>
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">工程部位/用途</span>
              </td>
              <td width="35%" class="text-center">
                <span>{{ form.gongcbwyt }}</span>
              </td>
            </tr>
            <tr>
              <td height="50" align="center">样品信息</td>
              <td class="specimen-border" colspan="3">
                <span>样品名称：</span>
                <span :class="{ placeholder: !form.yangpmc }">{{ form.yangpmc }}</span>
                <span>；样品编号：</span>
                <span :class="{ 'placeholder-long': !form.yangpbh }">{{ form.yangpbh }}</span>
                <span>；样品数量：</span>
                <span :class="{ placeholder: !form.yangpsl }">{{ form.yangpsl }}</span>
                <span>；样品状态：</span>
                <span :class="{ placeholder: !form.yangpzt }">{{ form.yangpzt }}</span>
                <span>；来样时间：</span>
                <span :class="{ placeholder: !form.yangpsj }">{{ form.yangpsj }}</span>
              </td>
            </tr>

            <tr>
              <td height="25" align="center" width="15%">
                <span style="width: 90px; display: inline-block">试验检测日期</span>
              </td>
               <!-- <td width="35%" class="text-center">{{ form.shiyjcrq }}</td> -->                                            
 <td width="35%" class="text-center">
      <el-input v-model="form.shiyjcrq" />
</td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">试验条件</span>
              </td>
              <td width="35%">
                <el-input v-model="form.shiytj" />
              </td>
            </tr>
            <tr>
              <td height="50" align="center" width="15%">
                <span style="width: 90px; display: inline-block">检测依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.jiancyj }}</span>
                <el-input v-else type="textarea" v-model="form.jiancyj" :rows="2" class="mark" @dblclick.native="showBsJiancyjView" readonly />
              </td>
              <td align="center" width="15%">
                <span style="width: 90px; display: inline-block">判定依据</span>
              </td>
              <td width="35%">
                <span v-if="isPdf">{{ form.pandyj }}</span>
                <el-input v-else type="textarea" v-model="form.pandyj" :rows="2" @dblclick.native="showBsJudgeBaseView" @focus="handleFocus('pandyj')" class="mark" />
              </td>
            </tr>
            <tr class="bottom-border">
              <td height="50" align="center">
                <div>主要仪器设备</div>
                <div>名称及编号</div>
              </td>
              <td colspan="3">
                <span v-if="isPdf">{{ form.zhuyyqsbmcjbh }}</span>
                <el-input v-else type="textarea" :rows="2" v-model="form.zhuyyqsbmcjbh" class="height100 width100 mark" @dblclick.native="showBsInstrumentView" readonly></el-input>
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .................................22222222222222222222222222222222............................................... -->
        <table height="616px" class="middle-table">
          <tbody>
            <tr height="25">
              <td>序号</td>
              <td>桩号</td>
              <td>平整度值(mm)</td>
              <td colspan="3">仪器采集打印结果</td>
            </tr>
            <tr height="25" v-for="(item, index) in form.detailList" :key="index">
              <td>
                <el-input type="text" v-model="item.xvh" @blur="setPzdBgData()" @keydown.native="handleKeyDown($event, index, 0)" :ref="`${index}_0`"/>
              </td>
              <td>
                <el-input type="text" v-model="item.zhuangh" @blur="setPzdBgData()"  @keydown.native="handleKeyDown($event, index, 1)" :ref="`${index}_1`"/>
              </td>
              <td>
                <el-input type="number" v-model="item.pingzdz" @blur="onCalculatePingjz()"  @keydown.native="handleKeyDown($event, index, 2)" :ref="`${index}_2`"/>
              </td>
              <td v-if="index == 0" colspan="3" rowspan="21"></td>
            </tr>
            <tr height="25">
              <td width="10%">设计值(mm)</td>
              <td width="30%">
                <el-input type="number" v-model="form.shejz" @blur="onCalculatePingjz()"  @keydown.native="handleKeyDown($event, 21, 0)" :ref="`21_0`"/>
              </td>
              <td width="10%">区间数</td>
              <td width="15%">
                <el-input type="number" v-model="form.qvjs" disabled />
              </td>
              <td width="20%">合格率(%)</td>
              <td width="15%">
                <el-input type="number" v-model="form.hegl" disabled />
              </td>
            </tr>
            <tr height="25">
              <td>平均值(mm)</td>
              <td>
                <el-input type="number" v-model="form.pingjz" disabled />
              </td>
              <td>标准差</td>
              <td>
                <el-input type="number" v-model="form.biaozc" disabled />
              </td>
              <td>变异系数(%)</td>
              <td>
                <el-input type="number" v-model="form.bianyxs" disabled />
              </td>
            </tr>
          </tbody>
        </table>
        <!-- .....................................................33333333333333333................................................... -->
        <table height="30px" class="bottom-table">
          <tr>
            <td>
              <span v-if="isPdf" class="textarea-span">{{ form.fujsm }}</span>
              <el-input v-else type="textarea" :rows="2" v-model="form.fujsm" @focus="handleFocus('fujsm')" />
            </td>
          </tr>
        </table>
      </div>
      <!-- .................................44444444444............................................. -->
      <table height="22px" class="transparent">
        <tbody>
          <tr>
            <td width="10%" align="right">检测：</td>
            <td width="15%" align="left"></td>
            <td width="10%" align="right">记录：</td>
            <td width="15%" align="left"></td>
            <td width="10%" align="right">复核：</td>
            <td align="left" width="15%"></td>
            <td width="10%">日期：</td>
            <td align="left" width="15%">
              <div style="display: inline-block; margin-left: 20px">年</div>
              <div style="display: inline-block; margin-left: 20px">月</div>
              <div style="display: inline-block ;margin-left: 20px">日</div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <bs-instrument-view ref="bsInstrumentViewRef" @select="selectInstrument"></bs-instrument-view>
    <bs-jiancyj-view ref="bsJiancyjViewRef" @select="selectJiancyj" :syType="syType"></bs-jiancyj-view>
    <bs-judge-base-view ref="bsJudgeBaseViewRef" @select="selectJudge" :syType="syType"></bs-judge-base-view>
  </div>
</template>

<script>
import { getPzdjlbBsInfo } from "@/api/report_ljlm/pzdjlbBsInfo";
import BsInstrumentView from "@/views/dept/bsInstrument/BsInstrumentView.vue";
import BaQjDetailView from "@/views/sycs/baQjDetail/BaQjDetailView.vue";
import BsJiancyjView from "@/views/sycs/bsJiancyj/BsJiancyjView.vue";
import BsJudgeBaseView from "@/views/sycs/bsJudgeBase/BsJudgeBaseView.vue";

import decimal from "@/utils/big-decimal";
import { handleFocus, emptyConvert, calculateMeanvalue, calculateStandardDeviation,handleKeyDown } from "@/views/reportForms/utils";
import Bus from "@/utils/bus";
export default {
  name: "pzdjlb",
  components: {
    BsInstrumentView,
    BaQjDetailView,
    BsJiancyjView,
    BsJudgeBaseView
  },
  props: {
    excelId: {
      type: [String, Number],
      default: () => ""
    },
    syType: {
      type: [String, Number],
      default: () => ""
    },
    page: {
      type: Number,
      default: () => 0
    },
    isPdf: {
      type: Boolean,
      default: () => false
    },
    printEmpty: {
      type: Boolean,
      default: () => false
    }
  },
  data() {
    return {
      params: {},
      query: {},
      // 表单参数
      form: {
        detailList: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
      }
    };
  },
  watch: {
    isPdf(newVal, oldVal) {
      this.form = { ...emptyConvert(this.form, newVal) };
    }
  },
  created() {
    this.getInfo();
  },
  mounted() {
    // 获取底部数据
    Bus.$on("setPzdBgDataBottom", (data) => {
      for (const key in data) {
        if (Object.prototype.hasOwnProperty.call(data, key)) {
          this.form[key] = data[key];
        }
      }
    });
  },

  beforeDestroy() {
    // 停止监听事件
    Bus.$off("setPzdBgDataBottom");
  },

  methods: {
    // 获取详情
    getInfo() {
      getPzdjlbBsInfo(this.excelId || this.$defaultExcelId).then((response) => {
        this.form = response.data;
        if (!this.form.fujsm) {
          this.form.fujsm = "附加声明：";
        }
      });
    },
    // 显示检测依据列表
    showBsJiancyjView() {
      this.$refs.bsJiancyjViewRef.init(this.form.jiancyjIds);
    },
    // 检测依据选择
    selectJiancyj(info) {
      this.form.jiancyj = info.label;
      this.form.jiancyjIds = info.ids;
      Bus.$emit("setPzdBgJiancyj", { jiancyj: this.form.jiancyj, jiancyjIds: this.form.jiancyjIds });
    },
    // 显示判定依据列表
    showBsJudgeBaseView() {
      this.$refs.bsJudgeBaseViewRef.init(this.form.pandyjIds);
    },
    // 判断依据选择
    selectJudge(info) {
       this.form.pandyjIds = info.ids;
      this.form.pandyj = info.label;
    },
    // 显示仪器列表
    showBsInstrumentView() {
      this.$refs.bsInstrumentViewRef.init(this.form.shebIds);
    },
    // 仪器选择
    selectInstrument(info) {
      this.form.zhuyyqsbmcjbh = info.label;
      this.form.shebIds = info.ids;
      Bus.$emit("setPzdBgZhuyyqsbmcjbh", { zhuyyqsbmcjbh: this.form.zhuyyqsbmcjbh, shebIds: this.form.shebIds });
    },
    onCalculatePingjz() {
      Bus.$emit("setPzdBgDataBottom", { shejz: this.form.shejz });
      this.$emit("getDetailList", "pzdjlb");
      this.setPzdBgData();
    },
    // 计算平均值
    calculateBg(detailList) {
      const shejz = this.form.shejz; // 设计值
      let qvjs = 0; // 区间数
      let hegds = 0; // 合格数
      let hegl = null; // 合格率
      let pingjz = null; // 平均值
      let biaozc = null; // 标准差
      let bianyxs = null; // 变异系数
      let arr = [];
      detailList.forEach((item) => {
        if (item.pingzdz) {
          arr.push(item.pingzdz);
          qvjs++;
        }
        if (item.pingzdz && shejz && item.pingzdz <= shejz) {
          hegds++;
        }
      });
      if (arr.length > 0) {
        pingjz = calculateMeanvalue(arr, 1);
        biaozc = calculateStandardDeviation(arr);
        // 变异系数= 标准差 / 平均值 * 100
        bianyxs = decimal.multiply(decimal.divide(biaozc, pingjz, 10), 100);
      }
      hegl = hegds > 0 ? decimal.multiply(decimal.divide(qvjs, hegds, 10), 100) : 0;
      this.form.qvjs = qvjs; // 区间数
      this.form.hegl = hegl; // 合格率
      this.form.pingjz = pingjz; // 平均值
      this.form.biaozc = biaozc; // 标准差
      this.form.bianyxs = bianyxs; // 变异系数
      this.setPzdBgDataBottom();
    },
    // 给报告传值
    setPzdBgData() {
      Bus.$emit("setPzdBgData" + this.page, this.form.detailList);
    },
    // 给报告传值
    setPzdBgDataBottom() {
      const info = {
        shejz: this.form.shejz,
        qvjs: this.form.qvjs,
        hegl: this.form.hegl,
        pingjz: this.form.pingjz,
        biaozc: this.form.biaozc,
        bianyxs: this.form.bianyxs
      };
      Bus.$emit("setPzdBgDataBottom", info);
    },
    submitForm() {
      return this.form;
    },
    handleFocus(event) {
      handleFocus(event, this.page, this.form[event]);
    },
    handleKeyDown(event, rowIndex, colIndex) {
      const maxArr = [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0];
      const ref = handleKeyDown(event, rowIndex, colIndex, maxArr);
      if (this.$refs[ref]?.length) {
        this.$refs[ref][0].focus();
      } else if (this.$refs[ref]) {
        this.$refs[ref].focus();
      }
    }
  }
};
</script>

<style scoped></style>
